//
//  BackUpBeesKeyViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/3.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "BackUpBeesKeyViewController.h"
#import "UITextView+WZB.h"

@interface BackUpBeesKeyViewController ()
@property (nonatomic,weak) UILabel *tipLab;
@property (nonatomic,weak) UIButton *copybtn;

@property (nonatomic,strong) UITextView *inputTV;
@property (nonatomic,assign) NSInteger steps;
@property (nonatomic,weak) UIView *successView;
@end

@implementation BackUpBeesKeyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
-(void)setSteps:(NSInteger)steps{
    _steps = steps;
    if (steps == 0) {
        self.copybtn.hidden = NO;
        self.inputTV.editable = NO;
        self.inputTV.text = self.info.private_key;
        self.tipLab.text = XLocalized(@"备份您的Bees私钥");
    }else if (steps == 1){
        self.copybtn.hidden = YES;
        self.inputTV.editable = YES;
        self.inputTV.text = @"";
        self.tipLab.text = XLocalized(@"确认您的私钥");
    }
}

- (void)backBtnClicked{
    [XAlertTool alertTitle:@"确认备份了私钥?" message:@"" cancelAction:^{
        
    } sureAction:^{
        [super  backBtnClicked];
    }];
}

- (void)setupUI{
    
    self.title = XLocalized(@"备份私钥");
    UILabel *tipLab = [UILabel new];
    self.tipLab = tipLab;
    tipLab.textColor = UIColorHex(#909090);
    tipLab.font = kDefaultFont(15);
    [self.contentView addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedHeight(45));
        make.left.mas_equalTo(AdaptedWidth(34));
    }];
    tipLab.text = XLocalized(@"备份您的Bees私钥");
    UIView *back = [UIView new];
    back.backgroundColor = UIColorHex(#F7F7F7);
    back.layer.cornerRadius = AdaptedWidth(10);
    [self.contentView addSubview:back];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedHeight(134));
        make.top.equalTo(tipLab.mas_bottom).offset(AdaptedHeight(12));
    }];
    
    self.inputTV = [UITextView new];
    
    [back addSubview:self.inputTV];
    [self.inputTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedHeight(30));
        make.bottom.mas_equalTo(AdaptedHeight(-35));
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(0);
    }];
    self.inputTV.backgroundColor = UIColorHex(#F7F7F7);
    self.inputTV.font = kDefaultFont(16);
    self.inputTV.textColor = UIColorHex(#666666);
    self.inputTV.editable = YES;
    self.inputTV.text = self.info.private_key;
    self.inputTV.contentInset = UIEdgeInsetsMake(0,AdaptedWidth(10), 0, AdaptedWidth(10));
    self.inputTV.wzb_placeholder = XLocalized(@"请输入您的私钥");
    self.inputTV.wzb_placeholderColor = UIColorHex(#B5B5B5);
    
    UIButton *copyBtn = [UIButton new];
    self.copybtn=copyBtn;
    [back addSubview:copyBtn];
    [copyBtn setImage:IMAGE_NAMED(@"copy_black") forState:UIControlStateNormal];
    [copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    kWeakSelf(self);
    [copyBtn addTapBlock:^(UIButton * _Nonnull btn) {
        if (weakself.inputTV.text.length) {
            UIPasteboard *pab = [UIPasteboard generalPasteboard];
            [pab setString:weakself.inputTV.text];
            if (pab == nil) {
                [weakself.view makeToast:XLocalized(@"复制失败!")];
            }else
            {
                [weakself.view makeToast:XLocalized(@"复制成功!")];
            }
        }
    }];
    
    
    UIView*seBack = [UIView new];
    seBack.backgroundColor = RGBA(252, 216, 57,.3);
    seBack.layer.cornerRadius = AdaptedWidth(10);
    [self.contentView addSubview:seBack];
    [seBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(back.mas_bottom).offset(AdaptedHeight(22));
        make.right.equalTo(back.mas_right);
        make.left.equalTo(back.mas_left);
    }];
    
    UIView*corner1 = [UIView new];
    corner1.backgroundColor = UIColorHex(666666);
    [seBack addSubview:corner1];
    [corner1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.top.mas_equalTo(kNormalSpace);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(7), AdaptedWidth(7)));
    }];
    corner1.layer.cornerRadius = AdaptedWidth(3.5);
    
    UILabel *subTip= [UILabel new];
    ViewRadius(subTip, AdaptedWidth(10));
    subTip.textColor = UIColorHex(#666666);
    subTip.font = kDefaultFont(13);
    [seBack addSubview:subTip];
    [subTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kNormalSpace);
        make.left.equalTo(corner1.mas_right).offset(kNormalSpace);
        make.top.equalTo(corner1.mas_top).offset(AdaptedHeight(-5));;
    }];
    subTip.numberOfLines = 0;
    subTip.text = XLocalized(@"请区分并备份好您的Bees私钥与钱包私钥。");
    
    UIView*corner2 = [UIView new];
    corner2.backgroundColor = UIColorHex(666666);
    [seBack addSubview:corner2];
    [corner2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.top.equalTo(subTip.mas_bottom).offset(kNormalSpace);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(7), AdaptedWidth(7)));
    }];
    corner2.layer.cornerRadius = AdaptedWidth(3.5);
    
    UILabel *subTip1= [UILabel new];
    ViewRadius(subTip1, AdaptedWidth(10));
    subTip1.textColor = UIColorHex(#666666);
    subTip1.font = kDefaultFont(13);
    [seBack addSubview:subTip1];
    [subTip1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kNormalSpace);
        make.left.equalTo(corner2.mas_right).offset(kNormalSpace);
        make.top.equalTo(corner2.mas_top).offset(AdaptedHeight(-5));;
        make.bottom.mas_equalTo(-kNormalSpace);
    }];
    subTip1.numberOfLines = 0;
    subTip1.text = XLocalized(@"该Bees私钥用于保护您的Bees账号不受损害，及找回您的账户密码，请妥善保管。");
    XThemeFlatButton*bottom = [XThemeFlatButton button];
    [bottom setTitle:XLocalized(@"下一步") forState:UIControlStateNormal];
    [self.contentView addSubview:bottom];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(back.mas_right);
        make.left.equalTo(back.mas_left);
        make.top.equalTo(seBack.mas_bottom).offset(AdaptedHeight(60));
        make.bottom.mas_equalTo(AdaptedHeight(-140));
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
//    The_hook
    
    UIView * successView= [UIView new];
    self.successView = successView;
    self.successView.hidden = YES;
    [self.contentView addSubview:successView];
    [successView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bottom.mas_top);
        make.top.left.right.mas_equalTo(0);
    }];
    YYLabel *successLab = [YYLabel new];
    NSString *str1 = XLocalized(@"您的Bees账号：");
    NSString *str2 = self.info.account;
    NSString* bottomText = [NSString stringWithFormat:@"%@%@",str1,str2];
    NSRange range = [bottomText rangeOfString:str2];
    NSMutableAttributedString *text  = [[NSMutableAttributedString alloc] initWithString:bottomText];
    [text setFont:kBoldFont(15) range:[text rangeOfAll]];
    text.color = UIColorHex(#B5B5B5);
    [text setTextHighlightRange:range color:UIColorHex(#FFCC03) backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
   
    }];
    successLab.attributedText = text;
    [successView addSubview:successLab];
    [successLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(AdaptedHeight(100));
    }];
    UIImageView*successImageV = [UIImageView new];
    successImageV.image = IMAGE_NAMED(@"The_hook");
    [successView addSubview:successImageV];
    [successImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(60));
        make.top.equalTo(successLab.mas_bottom).offset(AdaptedHeight(40));
    }];
    UILabel*successTipLab = [UILabel new];
    successTipLab.textColor = UIColorHex(#FFAE00);
    successTipLab.text = XLocalized(@"您的Bees账号已成功注册!");
    successTipLab.font = kDefaultFont(16);
    [successView addSubview:successTipLab];
    [successTipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(successImageV.mas_right).offset(AdaptedWidth(10));
        make.centerY.equalTo(successImageV.mas_centerY);
        make.right.mas_equalTo(-kNormalSpace);
    }];
    UIButton *copyBtn1 = [UIButton new];
    [successView addSubview:copyBtn1];
    [copyBtn1 setImage:IMAGE_NAMED(@"copy_black") forState:UIControlStateNormal];
    [copyBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(successLab.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(35, 35));
        make.centerY.equalTo(successLab.mas_centerY);
    }];
    [copyBtn1 addTapBlock:^(UIButton * _Nonnull btn) {
        if (weakself.info.account.length) {
            UIPasteboard *pab = [UIPasteboard generalPasteboard];
            [pab setString:weakself.info.account];
            if (pab == nil) {
                [weakself.view makeToast:XLocalized(@"复制失败!")];
            }else
            {
                [weakself.view makeToast:XLocalized(@"复制成功!")];
            }
        }
    }];
    
    
    
    
    
    [bottom addTapBlock:^(UIButton * _Nonnull btn) {
        if (weakself.steps == 0) {
            [XAlertTool alertTitle:@"再次确认" message:@"BEES不会上传您的账号私钥,一旦丢失无法找回,请妥善保管" cancelAction:^{
                
            } sureAction:^{
                weakself.steps = 1;
            }];
        }else if (weakself.steps==1){
            NSString*text = [weakself.inputTV text];
            if (!text.length) {
                [weakself.view makeToast:XLocalized(@"请输入您的私钥")];
                return;
            }
            if (![text isEqualToString:weakself.info.private_key]) {
                [XAlertTool alertTitle:@"备份的私钥不正确，是否重新查看私钥？" message:@"" cancelAction:^{
                    
                } sureAction:^{
                    weakself.steps = 0;
                }];
                return;
            }else{
                ///完成
                weakself.steps = 3;
                back.hidden = YES;
                seBack.hidden = YES;
                tipLab.hidden = YES;
                weakself.successView.hidden = NO;
                [btn setTitle:XLocalized(@"完成") forState:UIControlStateNormal];
            }
        }else{
            ///完成
            RootNavigationController*rootNav = (RootNavigationController*)weakself.navigationController;
            [rootNav popToAppointViewController:@"LoginViewController" animated:YES];
        }
    }];
    
    self.steps = 0;
}

@end
